home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 February / PCpro_2005_02.ISO / files / opensource / jEdit_4.2 / jedit42install.exe / {app} / jedit.jar / bsh / JavaCharStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-08-29  |  7.8 KB  |  495 lines

  1. package bsh;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.Reader;
  7.  
  8. public class JavaCharStream {
  9.    public static final boolean staticFlag = false;
  10.    public int bufpos;
  11.    int bufsize;
  12.    int available;
  13.    int tokenBegin;
  14.    protected int[] bufline;
  15.    protected int[] bufcolumn;
  16.    protected int column;
  17.    protected int line;
  18.    protected boolean prevCharIsCR;
  19.    protected boolean prevCharIsLF;
  20.    protected Reader inputStream;
  21.    protected char[] nextCharBuf;
  22.    protected char[] buffer;
  23.    protected int maxNextCharInd;
  24.    protected int nextCharInd;
  25.    protected int inBuf;
  26.  
  27.    static final int hexval(char c) throws IOException {
  28.       switch (c) {
  29.          case '0':
  30.             return 0;
  31.          case '1':
  32.             return 1;
  33.          case '2':
  34.             return 2;
  35.          case '3':
  36.             return 3;
  37.          case '4':
  38.             return 4;
  39.          case '5':
  40.             return 5;
  41.          case '6':
  42.             return 6;
  43.          case '7':
  44.             return 7;
  45.          case '8':
  46.             return 8;
  47.          case '9':
  48.             return 9;
  49.          case ':':
  50.          case ';':
  51.          case '<':
  52.          case '=':
  53.          case '>':
  54.          case '?':
  55.          case '@':
  56.          case 'G':
  57.          case 'H':
  58.          case 'I':
  59.          case 'J':
  60.          case 'K':
  61.          case 'L':
  62.          case 'M':
  63.          case 'N':
  64.          case 'O':
  65.          case 'P':
  66.          case 'Q':
  67.          case 'R':
  68.          case 'S':
  69.          case 'T':
  70.          case 'U':
  71.          case 'V':
  72.          case 'W':
  73.          case 'X':
  74.          case 'Y':
  75.          case 'Z':
  76.          case '[':
  77.          case '\\':
  78.          case ']':
  79.          case '^':
  80.          case '_':
  81.          case '`':
  82.          default:
  83.             throw new IOException();
  84.          case 'A':
  85.          case 'a':
  86.             return 10;
  87.          case 'B':
  88.          case 'b':
  89.             return 11;
  90.          case 'C':
  91.          case 'c':
  92.             return 12;
  93.          case 'D':
  94.          case 'd':
  95.             return 13;
  96.          case 'E':
  97.          case 'e':
  98.             return 14;
  99.          case 'F':
  100.          case 'f':
  101.             return 15;
  102.       }
  103.    }
  104.  
  105.    protected void ExpandBuff(boolean wrapAround) {
  106.       char[] newbuffer = new char[this.bufsize + 2048];
  107.       int[] newbufline = new int[this.bufsize + 2048];
  108.       int[] newbufcolumn = new int[this.bufsize + 2048];
  109.  
  110.       try {
  111.          if (wrapAround) {
  112.             System.arraycopy(this.buffer, this.tokenBegin, newbuffer, 0, this.bufsize - this.tokenBegin);
  113.             System.arraycopy(this.buffer, 0, newbuffer, this.bufsize - this.tokenBegin, this.bufpos);
  114.             this.buffer = newbuffer;
  115.             System.arraycopy(this.bufline, this.tokenBegin, newbufline, 0, this.bufsize - this.tokenBegin);
  116.             System.arraycopy(this.bufline, 0, newbufline, this.bufsize - this.tokenBegin, this.bufpos);
  117.             this.bufline = newbufline;
  118.             System.arraycopy(this.bufcolumn, this.tokenBegin, newbufcolumn, 0, this.bufsize - this.tokenBegin);
  119.             System.arraycopy(this.bufcolumn, 0, newbufcolumn, this.bufsize - this.tokenBegin, this.bufpos);
  120.             this.bufcolumn = newbufcolumn;
  121.             this.bufpos += this.bufsize - this.tokenBegin;
  122.          } else {
  123.             System.arraycopy(this.buffer, this.tokenBegin, newbuffer, 0, this.bufsize - this.tokenBegin);
  124.             this.buffer = newbuffer;
  125.             System.arraycopy(this.bufline, this.tokenBegin, newbufline, 0, this.bufsize - this.tokenBegin);
  126.             this.bufline = newbufline;
  127.             System.arraycopy(this.bufcolumn, this.tokenBegin, newbufcolumn, 0, this.bufsize - this.tokenBegin);
  128.             this.bufcolumn = newbufcolumn;
  129.             this.bufpos -= this.tokenBegin;
  130.          }
  131.       } catch (Throwable t) {
  132.          throw new Error(t.getMessage());
  133.       }
  134.  
  135.       this.available = this.bufsize += 2048;
  136.       this.tokenBegin = 0;
  137.    }
  138.  
  139.    protected void FillBuff() throws IOException {
  140.       if (this.maxNextCharInd == 4096) {
  141.          this.maxNextCharInd = this.nextCharInd = 0;
  142.       }
  143.  
  144.       try {
  145.          int i;
  146.          if ((i = this.inputStream.read(this.nextCharBuf, this.maxNextCharInd, 4096 - this.maxNextCharInd)) == -1) {
  147.             this.inputStream.close();
  148.             throw new IOException();
  149.          } else {
  150.             this.maxNextCharInd += i;
  151.          }
  152.       } catch (IOException e) {
  153.          if (this.bufpos != 0) {
  154.             --this.bufpos;
  155.             this.backup(0);
  156.          } else {
  157.             this.bufline[this.bufpos] = this.line;
  158.             this.bufcolumn[this.bufpos] = this.column;
  159.          }
  160.  
  161.          throw e;
  162.       }
  163.    }
  164.  
  165.    protected char ReadByte() throws IOException {
  166.       if (++this.nextCharInd >= this.maxNextCharInd) {
  167.          this.FillBuff();
  168.       }
  169.  
  170.       return this.nextCharBuf[this.nextCharInd];
  171.    }
  172.  
  173.    public char BeginToken() throws IOException {
  174.       if (this.inBuf > 0) {
  175.          --this.inBuf;
  176.          if (++this.bufpos == this.bufsize) {
  177.             this.bufpos = 0;
  178.          }
  179.  
  180.          this.tokenBegin = this.bufpos;
  181.          return this.buffer[this.bufpos];
  182.       } else {
  183.          this.tokenBegin = 0;
  184.          this.bufpos = -1;
  185.          return this.readChar();
  186.       }
  187.    }
  188.  
  189.    protected void AdjustBuffSize() {
  190.       if (this.available == this.bufsize) {
  191.          if (this.tokenBegin > 2048) {
  192.             this.bufpos = 0;
  193.             this.available = this.tokenBegin;
  194.          } else {
  195.             this.ExpandBuff(false);
  196.          }
  197.       } else if (this.available > this.tokenBegin) {
  198.          this.available = this.bufsize;
  199.       } else if (this.tokenBegin - this.available < 2048) {
  200.          this.ExpandBuff(true);
  201.       } else {
  202.          this.available = this.tokenBegin;
  203.       }
  204.  
  205.    }
  206.  
  207.    protected void UpdateLineColumn(char c) {
  208.       ++this.column;
  209.       if (this.prevCharIsLF) {
  210.          this.prevCharIsLF = false;
  211.          this.line += this.column = 1;
  212.       } else if (this.prevCharIsCR) {
  213.          this.prevCharIsCR = false;
  214.          if (c == '\n') {
  215.             this.prevCharIsLF = true;
  216.          } else {
  217.             this.line += this.column = 1;
  218.          }
  219.       }
  220.  
  221.       switch (c) {
  222.          case '\t':
  223.             --this.column;
  224.             this.column += 8 - (this.column & 7);
  225.             break;
  226.          case '\n':
  227.             this.prevCharIsLF = true;
  228.          case '\u000b':
  229.          case '\f':
  230.          default:
  231.             break;
  232.          case '\r':
  233.             this.prevCharIsCR = true;
  234.       }
  235.  
  236.       this.bufline[this.bufpos] = this.line;
  237.       this.bufcolumn[this.bufpos] = this.column;
  238.    }
  239.  
  240.    public char readChar() throws IOException {
  241.       if (this.inBuf > 0) {
  242.          --this.inBuf;
  243.          if (++this.bufpos == this.bufsize) {
  244.             this.bufpos = 0;
  245.          }
  246.  
  247.          return this.buffer[this.bufpos];
  248.       } else {
  249.          if (++this.bufpos == this.available) {
  250.             this.AdjustBuffSize();
  251.          }
  252.  
  253.          char c;
  254.          if ((this.buffer[this.bufpos] = c = this.ReadByte()) != '\\') {
  255.             this.UpdateLineColumn(c);
  256.             return c;
  257.          } else {
  258.             this.UpdateLineColumn(c);
  259.             int backSlashCnt = 1;
  260.  
  261.             while(true) {
  262.                if (++this.bufpos == this.available) {
  263.                   this.AdjustBuffSize();
  264.                }
  265.  
  266.                label80: {
  267.                   try {
  268.                      if ((this.buffer[this.bufpos] = c = this.ReadByte()) == '\\') {
  269.                         break label80;
  270.                      }
  271.  
  272.                      this.UpdateLineColumn(c);
  273.                      if (c != 'u' || (backSlashCnt & 1) != 1) {
  274.                         this.backup(backSlashCnt);
  275.                         return '\\';
  276.                      }
  277.  
  278.                      if (--this.bufpos < 0) {
  279.                         this.bufpos = this.bufsize - 1;
  280.                      }
  281.                   } catch (IOException var5) {
  282.                      if (backSlashCnt > 1) {
  283.                         this.backup(backSlashCnt);
  284.                      }
  285.  
  286.                      return '\\';
  287.                   }
  288.  
  289.                   try {
  290.                      while((c = this.ReadByte()) == 'u') {
  291.                         ++this.column;
  292.                      }
  293.  
  294.                      this.buffer[this.bufpos] = c = (char)(hexval(c) << 12 | hexval(this.ReadByte()) << 8 | hexval(this.ReadByte()) << 4 | hexval(this.ReadByte()));
  295.                      this.column += 4;
  296.                   } catch (IOException var4) {
  297.                      throw new Error("Invalid escape character at line " + this.line + " column " + this.column + ".");
  298.                   }
  299.  
  300.                   if (backSlashCnt == 1) {
  301.                      return c;
  302.                   }
  303.  
  304.                   this.backup(backSlashCnt - 1);
  305.                   return '\\';
  306.                }
  307.  
  308.                this.UpdateLineColumn(c);
  309.                ++backSlashCnt;
  310.             }
  311.          }
  312.       }
  313.    }
  314.  
  315.    /** @deprecated */
  316.    public int getColumn() {
  317.       return this.bufcolumn[this.bufpos];
  318.    }
  319.  
  320.    /** @deprecated */
  321.    public int getLine() {
  322.       return this.bufline[this.bufpos];
  323.    }
  324.  
  325.    public int getEndColumn() {
  326.       return this.bufcolumn[this.bufpos];
  327.    }
  328.  
  329.    public int getEndLine() {
  330.       return this.bufline[this.bufpos];
  331.    }
  332.  
  333.    public int getBeginColumn() {
  334.       return this.bufcolumn[this.tokenBegin];
  335.    }
  336.  
  337.    public int getBeginLine() {
  338.       return this.bufline[this.tokenBegin];
  339.    }
  340.  
  341.    public void backup(int amount) {
  342.       this.inBuf += amount;
  343.       if ((this.bufpos -= amount) < 0) {
  344.          this.bufpos += this.bufsize;
  345.       }
  346.  
  347.    }
  348.  
  349.    public JavaCharStream(Reader dstream, int startline, int startcolumn, int buffersize) {
  350.       this.bufpos = -1;
  351.       this.column = 0;
  352.       this.line = 1;
  353.       this.prevCharIsCR = false;
  354.       this.prevCharIsLF = false;
  355.       this.maxNextCharInd = 0;
  356.       this.nextCharInd = -1;
  357.       this.inBuf = 0;
  358.       this.inputStream = dstream;
  359.       this.line = startline;
  360.       this.column = startcolumn - 1;
  361.       this.available = this.bufsize = buffersize;
  362.       this.buffer = new char[buffersize];
  363.       this.bufline = new int[buffersize];
  364.       this.bufcolumn = new int[buffersize];
  365.       this.nextCharBuf = new char[4096];
  366.    }
  367.  
  368.    public JavaCharStream(Reader dstream, int startline, int startcolumn) {
  369.       this((Reader)dstream, startline, startcolumn, 4096);
  370.    }
  371.  
  372.    public JavaCharStream(Reader dstream) {
  373.       this((Reader)dstream, 1, 1, 4096);
  374.    }
  375.  
  376.    public void ReInit(Reader dstream, int startline, int startcolumn, int buffersize) {
  377.       this.inputStream = dstream;
  378.       this.line = startline;
  379.       this.column = startcolumn - 1;
  380.       if (this.buffer == null || buffersize != this.buffer.length) {
  381.          this.available = this.bufsize = buffersize;
  382.          this.buffer = new char[buffersize];
  383.          this.bufline = new int[buffersize];
  384.          this.bufcolumn = new int[buffersize];
  385.          this.nextCharBuf = new char[4096];
  386.       }
  387.  
  388.       this.prevCharIsLF = this.prevCharIsCR = false;
  389.       this.tokenBegin = this.inBuf = this.maxNextCharInd = 0;
  390.       this.nextCharInd = this.bufpos = -1;
  391.    }
  392.  
  393.    public void ReInit(Reader dstream, int startline, int startcolumn) {
  394.       this.ReInit((Reader)dstream, startline, startcolumn, 4096);
  395.    }
  396.  
  397.    public void ReInit(Reader dstream) {
  398.       this.ReInit((Reader)dstream, 1, 1, 4096);
  399.    }
  400.  
  401.    public JavaCharStream(InputStream dstream, int startline, int startcolumn, int buffersize) {
  402.       this((Reader)(new InputStreamReader(dstream)), startline, startcolumn, 4096);
  403.    }
  404.  
  405.    public JavaCharStream(InputStream dstream, int startline, int startcolumn) {
  406.       this((InputStream)dstream, startline, startcolumn, 4096);
  407.    }
  408.  
  409.    public JavaCharStream(InputStream dstream) {
  410.       this((InputStream)dstream, 1, 1, 4096);
  411.    }
  412.  
  413.    public void ReInit(InputStream dstream, int startline, int startcolumn, int buffersize) {
  414.       this.ReInit((Reader)(new InputStreamReader(dstream)), startline, startcolumn, 4096);
  415.    }
  416.  
  417.    public void ReInit(InputStream dstream, int startline, int startcolumn) {
  418.       this.ReInit((InputStream)dstream, startline, startcolumn, 4096);
  419.    }
  420.  
  421.    public void ReInit(InputStream dstream) {
  422.       this.ReInit((InputStream)dstream, 1, 1, 4096);
  423.    }
  424.  
  425.    public String GetImage() {
  426.       return this.bufpos >= this.tokenBegin ? new String(this.buffer, this.tokenBegin, this.bufpos - this.tokenBegin + 1) : new String(this.buffer, this.tokenBegin, this.bufsize - this.tokenBegin) + new String(this.buffer, 0, this.bufpos + 1);
  427.    }
  428.  
  429.    public char[] GetSuffix(int len) {
  430.       char[] ret = new char[len];
  431.       if (this.bufpos + 1 >= len) {
  432.          System.arraycopy(this.buffer, this.bufpos - len + 1, ret, 0, len);
  433.       } else {
  434.          System.arraycopy(this.buffer, this.bufsize - (len - this.bufpos - 1), ret, 0, len - this.bufpos - 1);
  435.          System.arraycopy(this.buffer, 0, ret, len - this.bufpos - 1, this.bufpos + 1);
  436.       }
  437.  
  438.       return ret;
  439.    }
  440.  
  441.    public void Done() {
  442.       this.nextCharBuf = null;
  443.       this.buffer = null;
  444.       this.bufline = null;
  445.       this.bufcolumn = null;
  446.    }
  447.  
  448.    public void adjustBeginLineColumn(int newLine, int newCol) {
  449.       int start = this.tokenBegin;
  450.       int len;
  451.       if (this.bufpos >= this.tokenBegin) {
  452.          len = this.bufpos - this.tokenBegin + this.inBuf + 1;
  453.       } else {
  454.          len = this.bufsize - this.tokenBegin + this.bufpos + 1 + this.inBuf;
  455.       }
  456.  
  457.       int i = 0;
  458.       int j = 0;
  459.       int k = 0;
  460.       int nextColDiff = 0;
  461.  
  462.       int columnDiff;
  463.       for(columnDiff = 0; i < len; ++i) {
  464.          int var10000 = this.bufline[j = start % this.bufsize];
  465.          ++start;
  466.          if (var10000 != this.bufline[k = start % this.bufsize]) {
  467.             break;
  468.          }
  469.  
  470.          this.bufline[j] = newLine;
  471.          nextColDiff = columnDiff + this.bufcolumn[k] - this.bufcolumn[j];
  472.          this.bufcolumn[j] = newCol + columnDiff;
  473.          columnDiff = nextColDiff;
  474.       }
  475.  
  476.       if (i < len) {
  477.          this.bufline[j] = newLine++;
  478.          this.bufcolumn[j] = newCol + columnDiff;
  479.  
  480.          while(i++ < len) {
  481.             int var14 = this.bufline[j = start % this.bufsize];
  482.             ++start;
  483.             if (var14 != this.bufline[start % this.bufsize]) {
  484.                this.bufline[j] = newLine++;
  485.             } else {
  486.                this.bufline[j] = newLine;
  487.             }
  488.          }
  489.       }
  490.  
  491.       this.line = this.bufline[j];
  492.       this.column = this.bufcolumn[j];
  493.    }
  494. }
  495.